/*!
 * Copyright (c) 2010 Jacob Essex
 *
 * Dual licenced under MIT or GPL 3 licneces
 * 
 * See the GPL3 or MIT files in the main directory for details
 */

require.def('pages', ['config', 'lib/hashstring', 'se/api', 'db', 'db/sites'],
function(Config, Hashstring, Api, Db, Sites){
    /**
     * Accessing the parent page is an issue, as it gives warnings that look ugly.
     * AFAIK it is a bug with chrome
     *
     * This expects the locations to be passed in the hash
     *
     * This is one of the few functions that returns, I may wanto to rethink it
     *
     */
    var getHost = function(){
        return Hashstring.get('host');
    }

    /**
     * This needs a callback as we need to hit the database to find the url of
     * the api
     */
    var getApi = function(callbacks){
        var siteDb = new Sites.SitesDb(Db.getDb(Config.SettingsDb));
        siteDb.getByHost(null, getHost(), {
            success:function(tx, site){
                callbacks.success && callbacks.success(
                    new Api.Api(site.ApiUrl, Config.ApiKey)
                );
            }
        });
    }

    return{
        'getHost':getHost,
        'getApi':getApi,
        
    };
});

